

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.feature.quantile &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.feature.quantile</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.feature.quantile</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python    </span>
<span class="c1"># -*- coding: utf-8 -*- </span>

<span class="c1">#</span>
<span class="c1">#  Copyright 2019 The FATE Authors. All Rights Reserved.</span>
<span class="c1">#</span>
<span class="c1">#  Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1">#  you may not use this file except in compliance with the License.</span>
<span class="c1">#  You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1">#  Unless required by applicable law or agreed to in writing, software</span>
<span class="c1">#  distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1">#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1">#  See the License for the specific language governing permissions and</span>
<span class="c1">#  limitations under the License.</span>
<span class="c1">#</span>
<span class="c1">################################################################################</span>
<span class="c1">#</span>
<span class="c1">#</span>
<span class="c1">################################################################################</span>

<span class="c1"># =============================================================================</span>
<span class="c1"># Quantile</span>
<span class="c1"># =============================================================================</span>

<span class="kn">from</span> <span class="nn">arch.api.utils</span> <span class="k">import</span> <span class="n">log_utils</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">from</span> <span class="nn">arch.api</span> <span class="k">import</span> <span class="n">eggroll</span>
<span class="kn">from</span> <span class="nn">federatedml.feature.sparse_vector</span> <span class="k">import</span> <span class="n">SparseVector</span>
<span class="kn">from</span> <span class="nn">federatedml.util</span> <span class="k">import</span> <span class="n">consts</span>

<span class="n">LOGGER</span> <span class="o">=</span> <span class="n">log_utils</span><span class="o">.</span><span class="n">getLogger</span><span class="p">()</span>

<span class="n">DEFAULT_BIN_GAP</span> <span class="o">=</span> <span class="mf">1e-6</span>
<span class="n">DEFAULT_BIN_NUM</span> <span class="o">=</span> <span class="mi">32</span>
<span class="n">DEFAULT_BIN_SAMPLE_NUM</span> <span class="o">=</span> <span class="mi">10000</span>


<div class="viewcode-block" id="Quantile"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile">[docs]</a><span class="k">class</span> <span class="nc">Quantile</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="k">pass</span>

<div class="viewcode-block" id="Quantile.convert_feature_to_bin"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.convert_feature_to_bin">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">convert_feature_to_bin</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">method</span><span class="p">,</span> <span class="n">bin_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_NUM</span><span class="p">,</span>
                               <span class="n">bin_gap</span><span class="o">=</span><span class="n">DEFAULT_BIN_GAP</span><span class="p">,</span> <span class="n">bin_sample_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_SAMPLE_NUM</span><span class="p">,</span>
                               <span class="n">valid_features</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Convert instance&#39;s features to binning, use for secureboost only in this version</span>


<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instance : DTable</span>
<span class="sd">            The input data</span>

<span class="sd">        method : str, accepted &quot;bin_by_sample_data&quot;, &quot;bin_by_data_block&quot; only</span>
<span class="sd">            if method is &quot;bin_by_sample_data&quot;, if will sample bin_sample_num amount of data,</span>
<span class="sd">                then find split points in such data.</span>
<span class="sd">            if method is &quot;bin_by_data_block&quot;, it will generated split points in each partition of data firstly,</span>
<span class="sd">                then merge split points of all partition and genenerate final split points</span>

<span class="sd">        bin_num : int, max num of bins each column will generate</span>

<span class="sd">        bin_gap : float, the least gap of any two adjacent bin split points</span>

<span class="sd">        bin_sample_num : int, use when method is &quot;bin_by_sample_data&quot;</span>

<span class="sd">        valid_features : None or list,</span>
<span class="sd">            if valid_features is not None, it will specify which columns need to binning</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        data_bin: DTable,</span>
<span class="sd">            instance, whose features were converted to bins</span>

<span class="sd">        bin_split_points: 2D numpy&#39;s ndarray,</span>
<span class="sd">            split points of each feature need to binning</span>

<span class="sd">        bin_sparse_points: 1D numpy&#39;s ndarray,</span>
<span class="sd">            use for sparse representation, which bin is 0 locate for each feature</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;begin to fconvert feature to bin&quot;</span><span class="p">)</span>
        <span class="n">bin_split_points</span> <span class="o">=</span> <span class="n">Quantile</span><span class="o">.</span><span class="n">find_bin_split_points</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">method</span><span class="p">,</span> <span class="n">bin_num</span><span class="p">,</span>
                                                          <span class="n">bin_gap</span><span class="p">,</span> <span class="n">bin_sample_num</span><span class="p">,</span> <span class="n">valid_features</span><span class="p">)</span>

        <span class="n">bin_split_points</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">)</span>
        <span class="n">bin_sparse_points</span> <span class="o">=</span> <span class="n">Quantile</span><span class="o">.</span><span class="n">find_bin_sparse_points</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">)</span>

        <span class="n">convert_bins</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Quantile</span><span class="o">.</span><span class="n">convert_instance_to_bin</span><span class="p">,</span> <span class="n">bin_split_points</span><span class="o">=</span><span class="n">bin_split_points</span><span class="p">)</span>
        <span class="n">data_bin</span> <span class="o">=</span> <span class="n">data_instance</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="n">convert_bins</span><span class="p">)</span>

        <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;end to fconvert feature to bin&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">data_bin</span><span class="p">,</span> <span class="n">bin_split_points</span><span class="p">,</span> <span class="n">bin_sparse_points</span></div>

<div class="viewcode-block" id="Quantile.find_bin_sparse_points"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.find_bin_sparse_points">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">find_bin_sparse_points</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find out which bin is 0 should be locate for every feature.</span>

<span class="sd">        If split points is no more than 20, will use brute-force,</span>
<span class="sd">        else use binary search instead</span>


<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        bin_split_points : 2D numpy&#39;s ndarray</span>
<span class="sd">            the split points of every column</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        bin_sparse_points: 1D numpy&#39;s ndarray,</span>
<span class="sd">            which bin should 0 be for every feature column</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;find sparse points of bin&quot;</span><span class="p">)</span>
        <span class="n">bin_sparse_points</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">bin_split_points</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])]</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">bin_split_points</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
            <span class="k">if</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="n">pos</span> <span class="o">=</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mi">20</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                    <span class="k">if</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">j</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="n">consts</span><span class="o">.</span><span class="n">FLOAT_ZERO</span><span class="p">:</span>
                        <span class="n">pos</span> <span class="o">=</span> <span class="n">j</span>
                        <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">l</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">r</span> <span class="o">=</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span>
                <span class="k">while</span> <span class="n">l</span> <span class="o">&lt;=</span> <span class="n">r</span><span class="p">:</span>
                    <span class="n">mid</span> <span class="o">=</span> <span class="p">(</span><span class="n">l</span> <span class="o">+</span> <span class="n">r</span><span class="p">)</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span>
                    <span class="k">if</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">mid</span><span class="p">]</span> <span class="o">&gt;=</span> <span class="n">consts</span><span class="o">.</span><span class="n">FLOAT_ZERO</span><span class="p">:</span>
                        <span class="n">pos</span> <span class="o">=</span> <span class="n">mid</span>
                        <span class="n">r</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">-</span> <span class="mi">1</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">l</span> <span class="o">=</span> <span class="n">mid</span> <span class="o">+</span> <span class="mi">1</span>

            <span class="n">bin_sparse_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">pos</span>

        <span class="k">return</span> <span class="n">bin_sparse_points</span></div>

<div class="viewcode-block" id="Quantile.find_bin_split_points"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.find_bin_split_points">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">find_bin_split_points</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">method</span><span class="p">,</span> <span class="n">bin_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_NUM</span><span class="p">,</span>
                              <span class="n">bin_gap</span><span class="o">=</span><span class="n">DEFAULT_BIN_GAP</span><span class="p">,</span> <span class="n">bin_sample_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_SAMPLE_NUM</span><span class="p">,</span>
                              <span class="n">valid_features</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;find bin split points&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">method</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;quantile method should be a str!!!&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">method</span> <span class="o">==</span> <span class="s2">&quot;bin_by_data_block&quot;</span><span class="p">:</span>
            <span class="n">bin_split_points</span> <span class="o">=</span> <span class="n">Quantile</span><span class="o">.</span><span class="n">gen_bin_by_merge_data_block</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">bin_num</span><span class="p">,</span> <span class="n">bin_gap</span><span class="p">,</span> <span class="n">valid_features</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">method</span> <span class="o">==</span> <span class="s2">&quot;bin_by_sample_data&quot;</span><span class="p">:</span>
            <span class="n">bin_split_points</span> <span class="o">=</span> <span class="n">Quantile</span><span class="o">.</span><span class="n">gen_bin_by_sample_data</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">bin_num</span><span class="p">,</span>
                                                               <span class="n">bin_gap</span><span class="p">,</span> <span class="n">bin_sample_num</span><span class="p">,</span> <span class="n">valid_features</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;quantile method </span><span class="si">%s</span><span class="s2"> is not support yes!!&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">method</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">bin_split_points</span></div>

<div class="viewcode-block" id="Quantile.gen_bin_by_merge_data_block"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.gen_bin_by_merge_data_block">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">gen_bin_by_merge_data_block</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">bin_num</span><span class="p">,</span> <span class="n">bin_gap</span><span class="p">,</span> <span class="n">valid_features</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find out bin split points by firstly use mapParitions interface to get</span>
<span class="sd">            split points of data partition, then merge all split points to</span>
<span class="sd">            generate final split points.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instance : DTable</span>
<span class="sd">            value of each object is instance define in federatedml.feature.instance</span>

<span class="sd">        bin_num : int, max number of bins each column feature will generate</span>

<span class="sd">        bin_gap : float, least gap of two adjacent split points should have</span>

<span class="sd">        bin_sample_num : int, max number of data to be sample to generate bin split points</span>

<span class="sd">        valid_features: None or list,</span>
<span class="sd">            if valid_features is not None, it will specify which columns need to binning</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        bin_sparse_points: 1D numpy&#39;s ndarray,</span>
<span class="sd">            which bin should 0 be for every feature column</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;fgen bin split points by merge data block&quot;</span><span class="p">)</span>

        <span class="n">generate_bin_by_batch_func</span> <span class="o">=</span> <span class="n">functools</span><span class="o">.</span><span class="n">partial</span><span class="p">(</span><span class="n">Quantile</span><span class="o">.</span><span class="n">generate_bin_by_batch</span><span class="p">,</span>
                                                       <span class="p">[</span><span class="n">bin_num</span><span class="p">,</span> <span class="n">bin_gap</span><span class="p">,</span> <span class="n">valid_features</span><span class="p">])</span>
        <span class="n">distributed_sample_bins</span> <span class="o">=</span> <span class="n">data_instance</span><span class="o">.</span><span class="n">mapPartitions</span><span class="p">(</span><span class="n">generate_bin_by_batch_func</span><span class="p">)</span>

        <span class="n">bin_split_point_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">bin_split_points</span> <span class="k">for</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">bin_split_points</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">list</span><span class="p">(</span><span class="n">distributed_sample_bins</span><span class="o">.</span><span class="n">collect</span><span class="p">())]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">bin_split_point_list</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;no sample bins find!!!&quot;</span><span class="p">)</span>

        <span class="n">bin_split_points</span> <span class="o">=</span> <span class="n">Quantile</span><span class="o">.</span><span class="n">merge_bin_split_points</span><span class="p">(</span><span class="n">bin_split_point_list</span><span class="p">,</span> <span class="n">bin_num</span><span class="p">,</span> <span class="n">bin_gap</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">bin_split_points</span></div>

<div class="viewcode-block" id="Quantile.generate_bin_by_batch"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.generate_bin_by_batch">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">generate_bin_by_batch</span><span class="p">(</span><span class="n">param_list</span><span class="p">,</span> <span class="n">key_value_tuples</span><span class="p">):</span>
        <span class="n">samples</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">non_empty_data_block</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">instance</span> <span class="ow">in</span> <span class="n">key_value_tuples</span><span class="p">:</span>
            <span class="n">samples</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span>
            <span class="n">non_empty_data_block</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">non_empty_data_block</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;data block has no data!!!&quot;</span><span class="p">)</span>

        <span class="n">bin_num</span><span class="p">,</span> <span class="n">bin_gap</span><span class="p">,</span> <span class="n">valid_features</span> <span class="o">=</span> <span class="n">param_list</span>
        <span class="k">return</span> <span class="n">Quantile</span><span class="o">.</span><span class="n">gen_bin_by_data_block</span><span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">bin_num</span><span class="p">,</span> <span class="n">bin_gap</span><span class="p">,</span> <span class="n">valid_features</span><span class="p">)</span></div>

<div class="viewcode-block" id="Quantile.merge_bin_split_points"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.merge_bin_split_points">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">merge_bin_split_points</span><span class="p">(</span><span class="n">bin_split_point_list</span><span class="p">,</span> <span class="n">bin_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_NUM</span><span class="p">,</span> <span class="n">bin_gap</span><span class="o">=</span><span class="n">DEFAULT_BIN_GAP</span><span class="p">):</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;fmerge bin split points&quot;</span><span class="p">)</span>

        <span class="n">feature_num</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">bin_split_point_list</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">all_bin_split_points</span> <span class="o">=</span> <span class="p">[[]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">feature_num</span><span class="p">)]</span>
        <span class="k">for</span> <span class="n">bin_split_points</span> <span class="ow">in</span> <span class="n">bin_split_point_list</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">feature_num</span><span class="p">):</span>
                <span class="n">all_bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>

        <span class="n">all_bin_split_points</span> <span class="o">=</span> <span class="p">[</span><span class="nb">sorted</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">)</span> <span class="k">for</span> <span class="n">bin_split_points</span> <span class="ow">in</span> <span class="n">all_bin_split_points</span><span class="p">]</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">feature_num</span><span class="p">):</span>
            <span class="n">split_points</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">bin_split_points</span> <span class="o">=</span> <span class="n">all_bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="n">split_bin_num</span> <span class="o">=</span> <span class="mi">0</span>

            <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">)):</span>
                <span class="k">if</span> <span class="n">j</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">np</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">-</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">j</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="n">bin_gap</span><span class="p">:</span>
                    <span class="n">split_points</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">[</span><span class="n">j</span><span class="p">])</span>
                    <span class="n">split_bin_num</span> <span class="o">+=</span> <span class="mi">1</span>

            <span class="k">if</span> <span class="n">split_bin_num</span> <span class="o">&gt;</span> <span class="n">bin_num</span><span class="p">:</span>
                <span class="n">split_points</span> <span class="o">=</span> <span class="p">[</span><span class="n">split_points</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span>
                                <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">split_bin_num</span><span class="p">,</span> <span class="p">(</span><span class="n">split_bin_num</span> <span class="o">+</span> <span class="n">bin_num</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">//</span> <span class="n">bin_num</span><span class="p">)]</span>

            <span class="n">all_bin_split_points</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">split_points</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">all_bin_split_points</span><span class="p">)</span></div>

<div class="viewcode-block" id="Quantile.gen_bin_by_sample_data"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.gen_bin_by_sample_data">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">gen_bin_by_sample_data</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">bin_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_NUM</span><span class="p">,</span> <span class="n">bin_gap</span><span class="o">=</span><span class="n">DEFAULT_BIN_GAP</span><span class="p">,</span>
                               <span class="n">bin_sample_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_NUM</span><span class="p">,</span> <span class="n">valid_features</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find out bin split points by firstly sample bin_sample_num amount of data,</span>
<span class="sd">            then use these data to generate bin split points.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instance : DTable</span>
<span class="sd">            value of each object is instance define in federatedml.feature.instance</span>

<span class="sd">        bin_num : int, max number of bins each column feature will generate</span>

<span class="sd">        bin_gap : float, least gap of two adjacent split points should have</span>

<span class="sd">        valid_features : None or list,</span>
<span class="sd">            if valid_features is not None, it will specify which columns need to binning</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        bin_sparse_points : 1D numpy&#39;s ndarray,</span>
<span class="sd">            which bin should 0 be for every feature column</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;gen bin by sample data set&quot;</span><span class="p">)</span>
        <span class="n">sample_datas</span> <span class="o">=</span> <span class="n">Quantile</span><span class="o">.</span><span class="n">sample_data</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">bin_sample_num</span><span class="p">)</span>

        <span class="n">samples</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">block_data</span> <span class="ow">in</span> <span class="n">sample_datas</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">block_data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">samples</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">block_data</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">Quantile</span><span class="o">.</span><span class="n">gen_bin_by_data_block</span><span class="p">(</span><span class="n">samples</span><span class="p">,</span> <span class="n">bin_num</span><span class="p">,</span> <span class="n">bin_gap</span><span class="p">,</span> <span class="n">valid_features</span><span class="p">)</span></div>

<div class="viewcode-block" id="Quantile.gen_bin_by_data_block"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.gen_bin_by_data_block">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">gen_bin_by_data_block</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">bin_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_NUM</span><span class="p">,</span> <span class="n">bin_gap</span><span class="o">=</span><span class="n">DEFAULT_BIN_GAP</span><span class="p">,</span> <span class="n">valid_features</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Find out bin split points of data</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data : list, element of the list is instance define in federatedml.feature.instance</span>

<span class="sd">        bin_num: int, max number of bins each column feature will generate</span>

<span class="sd">        bin_gap: float, least gap of two adjacent split points should have</span>

<span class="sd">        valid_features: None or list,</span>
<span class="sd">            if valid_features is not None, it will specify which columns need to binning</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        bin_sparse_points: 1D numpy&#39;s ndarray,</span>
<span class="sd">            which bin should 0 be for every feature column</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">bin_split_points</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">sparse_data</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;ndarray&quot;</span><span class="p">:</span>
            <span class="n">feature_num</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">feature_num</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">get_shape</span><span class="p">()</span>
            <span class="n">sparse_data</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="n">data_num</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

        <span class="n">all_features</span> <span class="o">=</span> <span class="p">[[]</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">feature_num</span><span class="p">)]</span>
        <span class="k">if</span> <span class="n">sparse_data</span><span class="p">:</span>
            <span class="n">zero_count</span> <span class="o">=</span> <span class="p">[</span><span class="n">data_num</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">feature_num</span><span class="p">)]</span>

        <span class="k">for</span> <span class="n">_data</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
            <span class="n">features</span> <span class="o">=</span> <span class="n">_data</span><span class="o">.</span><span class="n">features</span>

            <span class="k">if</span> <span class="n">sparse_data</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">fid</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">features</span><span class="o">.</span><span class="n">get_all_data</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">valid_features</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">fid</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">valid_features</span><span class="p">:</span>
                        <span class="k">continue</span>

                    <span class="n">all_features</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">val</span><span class="p">)</span>
                    <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span> <span class="o">-=</span> <span class="mi">1</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">fid</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">feature_num</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">valid_features</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">fid</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">valid_features</span><span class="p">:</span>
                        <span class="k">continue</span>

                    <span class="n">all_features</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">features</span><span class="p">[</span><span class="n">fid</span><span class="p">])</span>

        <span class="k">for</span> <span class="n">fid</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">feature_num</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">valid_features</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">fid</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">valid_features</span><span class="p">:</span>
                <span class="n">bin_split_points</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([]))</span>
                <span class="k">continue</span>

            <span class="n">feature_values</span> <span class="o">=</span> <span class="n">all_features</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span>

            <span class="k">if</span> <span class="n">sparse_data</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
                <span class="n">distinct_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="n">feature_values</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">feature_values</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

                <span class="n">distinct_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="n">feature_values</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">feature_values</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>

            <span class="n">distincts</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">distinct_count</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="n">distincts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">distinct_values</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">distinct_values</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">distinct_values</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">distincts</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">&lt;</span> <span class="n">bin_gap</span><span class="p">:</span>
                    <span class="k">pass</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">distinct_count</span> <span class="o">+=</span> <span class="mi">1</span>
                    <span class="n">distincts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">distinct_values</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

            <span class="k">if</span> <span class="n">distinct_count</span> <span class="o">&lt;=</span> <span class="n">bin_num</span><span class="p">:</span>
                <span class="n">bin_split_points</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">distincts</span><span class="p">))</span>
                <span class="k">continue</span>

            <span class="k">if</span> <span class="n">sparse_data</span> <span class="ow">and</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span> <span class="o">==</span> <span class="n">data_num</span><span class="p">:</span>
                    <span class="n">bin_split_points</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">([</span><span class="mi">0</span><span class="p">]))</span>
                    <span class="k">continue</span>

                <span class="n">feature_values</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">feature_values</span><span class="p">)</span>
                <span class="n">negative</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="n">positions</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">data_num</span> <span class="o">*</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="n">bin_num</span> <span class="o">*</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">bin_num</span><span class="p">)]</span>

                <span class="n">percentiles</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">if</span> <span class="n">feature_values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mf">0.0</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">pos</span> <span class="ow">in</span> <span class="n">positions</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">pos</span> <span class="o">&lt;=</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]:</span>
                            <span class="n">percentiles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">percentiles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feature_values</span><span class="p">[</span><span class="n">pos</span> <span class="o">-</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span>
                <span class="k">elif</span> <span class="n">feature_values</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">pos</span> <span class="ow">in</span> <span class="n">positions</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">pos</span> <span class="o">&lt;</span> <span class="n">data_num</span> <span class="o">-</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]:</span>
                            <span class="n">percentiles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feature_values</span><span class="p">[</span><span class="n">pos</span><span class="p">])</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">percentiles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">feature_values</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                            <span class="n">negative</span> <span class="o">+=</span> <span class="mi">1</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">break</span>
                    <span class="k">for</span> <span class="n">pos</span> <span class="ow">in</span> <span class="n">positions</span><span class="p">:</span>
                        <span class="k">if</span> <span class="n">pos</span> <span class="o">&lt;</span> <span class="n">negative</span><span class="p">:</span>
                            <span class="n">percentiles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feature_values</span><span class="p">[</span><span class="n">pos</span><span class="p">])</span>
                        <span class="k">elif</span> <span class="n">pos</span> <span class="o">&lt;</span> <span class="n">negative</span> <span class="o">+</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]:</span>
                            <span class="n">percentiles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">percentiles</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">feature_values</span><span class="p">[</span><span class="n">pos</span> <span class="o">-</span> <span class="n">zero_count</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span> <span class="o">-</span> <span class="n">negative</span><span class="p">])</span>
                    <span class="n">percentiles</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">percentiles</span><span class="p">)</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="n">percentile_list</span> <span class="o">=</span> <span class="p">[</span><span class="mf">100.0</span> <span class="o">/</span> <span class="n">bin_num</span> <span class="o">*</span> <span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">bin_num</span><span class="p">)]</span>
                <span class="n">percentiles</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">percentile</span><span class="p">(</span><span class="n">feature_values</span><span class="p">,</span> <span class="n">percentile_list</span><span class="p">)</span>

            <span class="n">percentiles</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">percentiles</span><span class="p">)</span>
            <span class="n">split_points</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">percentiles</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">np</span><span class="o">.</span><span class="n">fabs</span><span class="p">(</span><span class="n">percentiles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-</span> <span class="n">percentiles</span><span class="p">[</span><span class="n">i</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="n">bin_gap</span><span class="p">:</span>
                    <span class="n">split_points</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">percentiles</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

            <span class="n">bin_split_points</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">split_points</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">bin_split_points</span></div>

<div class="viewcode-block" id="Quantile.sample_data"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.sample_data">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">sample_data</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="n">bin_sample_num</span><span class="o">=</span><span class="n">DEFAULT_BIN_SAMPLE_NUM</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        sample data from a dtable</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        data_instance : DTable</span>
<span class="sd">            The input data</span>

<span class="sd">        bin_sample_num : int, max number of data to be sample to generate bin split points</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sample_data: list, element is a (id, instance) tuple</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">LOGGER</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;fsample data set&quot;</span><span class="p">)</span>

        <span class="n">data_key_none_value</span> <span class="o">=</span> <span class="n">data_instance</span><span class="o">.</span><span class="n">mapValues</span><span class="p">(</span><span class="k">lambda</span> <span class="n">value</span><span class="p">:</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">data_key_none_value_tuple</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">data_key_none_value</span><span class="o">.</span><span class="n">collect</span><span class="p">())</span>

        <span class="n">data_num</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_key_none_value_tuple</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">data_num</span> <span class="o">&lt;=</span> <span class="n">bin_sample_num</span><span class="p">:</span>
            <span class="n">data_keys</span> <span class="o">=</span> <span class="p">[(</span><span class="n">key</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="ow">in</span> <span class="n">data_key_none_value_tuple</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sample_idxs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">data_num</span><span class="p">,</span> <span class="n">bin_sample_num</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">data_keys</span> <span class="o">=</span> <span class="p">[</span><span class="n">data_key_none_value_tuple</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">sample_idxs</span><span class="p">]</span>

        <span class="n">data_key_table</span> <span class="o">=</span> <span class="n">eggroll</span><span class="o">.</span><span class="n">parallelize</span><span class="p">(</span><span class="n">data_keys</span><span class="p">,</span> <span class="n">include_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">sample_data</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">data_key_table</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">data_instance</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">y</span><span class="p">)</span><span class="o">.</span><span class="n">collect</span><span class="p">())</span>
        <span class="k">return</span> <span class="n">sample_data</span></div>

<div class="viewcode-block" id="Quantile.convert_instance_to_bin"><a class="viewcode-back" href="../../../federatedml.feature.html#federatedml.feature.quantile.Quantile.convert_instance_to_bin">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">convert_instance_to_bin</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">bin_split_points</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method use by mapValues Api, convert an instance object&#39;s features to bins</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        instance : Instance Object</span>

<span class="sd">        bin_split_points: 2D numpy&#39;s ndarray,</span>
<span class="sd">            split points of each feature need to binning</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        instance: Instance Object, the instance object&#39;s features converted to bins</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">sparse_data</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="n">features</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;ndarray&quot;</span><span class="p">:</span>
            <span class="n">feature_shape</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">feature_shape</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">get_shape</span><span class="p">()</span>
            <span class="n">sparse_data</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">indices</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="n">data_format</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="n">features</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span>

        <span class="n">features</span> <span class="o">=</span> <span class="n">instance</span><span class="o">.</span><span class="n">features</span>

        <span class="n">bins</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="k">if</span> <span class="n">sparse_data</span><span class="p">:</span>
            <span class="n">feature_values</span> <span class="o">=</span> <span class="p">[</span><span class="n">kv</span> <span class="k">for</span> <span class="n">kv</span> <span class="ow">in</span> <span class="n">features</span><span class="o">.</span><span class="n">get_all_data</span><span class="p">()]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">feature_values</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">features</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">features</span><span class="o">.</span><span class="n">tolist</span><span class="p">()))</span>

        <span class="k">for</span> <span class="n">fid</span><span class="p">,</span> <span class="n">feature_value</span> <span class="ow">in</span> <span class="n">feature_values</span><span class="p">:</span>
            <span class="n">bin_id</span> <span class="o">=</span> <span class="mi">0</span>

            <span class="k">if</span> <span class="n">sparse_data</span><span class="p">:</span>
                <span class="n">indices</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fid</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">bins</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bin_id</span><span class="p">)</span>
                <span class="k">continue</span>

            <span class="k">if</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;=</span> <span class="mi">20</span><span class="p">:</span>
                <span class="n">bin_id</span> <span class="o">=</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                    <span class="k">if</span> <span class="n">feature_value</span> <span class="o">&lt;=</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">][</span><span class="n">idx</span><span class="p">]:</span>
                        <span class="n">bin_id</span> <span class="o">=</span> <span class="n">idx</span>
                        <span class="k">break</span>

                <span class="n">bins</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bin_id</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">feature_value</span> <span class="o">&lt;=</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">][</span><span class="mi">0</span><span class="p">]:</span>
                    <span class="n">bin_id</span> <span class="o">=</span> <span class="mi">0</span>
                <span class="k">elif</span> <span class="n">feature_value</span> <span class="o">&gt;</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">][</span><span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]:</span>
                    <span class="n">bin_id</span> <span class="o">=</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">left</span> <span class="o">=</span> <span class="mi">0</span>
                    <span class="n">right</span> <span class="o">=</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span>
                    <span class="k">while</span> <span class="n">left</span> <span class="o">&lt;=</span> <span class="n">right</span><span class="p">:</span>
                        <span class="n">idx</span> <span class="o">=</span> <span class="p">(</span><span class="n">left</span> <span class="o">+</span> <span class="n">right</span><span class="p">)</span> <span class="o">&gt;&gt;</span> <span class="mi">1</span>

                        <span class="k">if</span> <span class="n">feature_value</span> <span class="o">&lt;=</span> <span class="n">bin_split_points</span><span class="p">[</span><span class="n">fid</span><span class="p">][</span><span class="n">idx</span><span class="p">]:</span>
                            <span class="n">bin_id</span> <span class="o">=</span> <span class="n">idx</span>
                            <span class="n">right</span> <span class="o">=</span> <span class="n">idx</span> <span class="o">-</span> <span class="mi">1</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="n">left</span> <span class="o">=</span> <span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span>

                <span class="n">bins</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bin_id</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">data_format</span> <span class="o">==</span> <span class="s2">&quot;ndarray&quot;</span><span class="p">:</span>
            <span class="n">instance</span><span class="o">.</span><span class="n">features</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">bins</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;int&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">instance</span><span class="o">.</span><span class="n">features</span> <span class="o">=</span> <span class="n">SparseVector</span><span class="p">(</span><span class="n">indices</span><span class="p">,</span> <span class="n">bins</span><span class="p">,</span> <span class="n">feature_shape</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">instance</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>